package org.swfer.UIC 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	/**
	 * 下拉列表
	 * 必须部件 valueButton,list
	 * 回调事件 change
	 * @author swfer
	 */
	public class Combox extends Component 
	{
		public static const CHANGE:String = 'change';
		
		protected var valueButton:Button;
		protected var _list:List;
		
		public function Combox(view:MovieClip) 
		{
			super(view);
		}
		
		/////////////////////////////////////
		//method
		/////////////////////////////////////
		
		override public function dispose():void
		{
			if (disposed) return;
			super.dispose();
			
			_list.dispose();
			valueButton.dispose();
		}
		
		override protected function init():void
		{
			_list = new List(view.getChildByName('list') as MovieClip);
			_list.view.visible = false;
			
			valueButton = new Button(view.getChildByName('valueButton') as MovieClip);
		}
		
		override protected function addEvt():void
		{
			_list.addCallBack(List.SELECT, onListSelect);
			valueButton.view.addEventListener(MouseEvent.CLICK,onValueButtonClick);
		}
		
		override protected function removeEvt():void
		{
			_list.removeAllCallBack();
			view.stage.removeEventListener(MouseEvent.MOUSE_DOWN,hideList);
			valueButton.view.removeEventListener.(MouseEvent.CLICK,onValueButtonClick);
		}
		
		private function onListSelect(target:List):void
		{
			if (valueButton.injectMethod !=null) valueButton.injectData = target.selectedDataItem;
			else injectMx(valueButton.view, target.selectedDataItem);
			
			executeCallBack(CHANGE);
			
			view.stage.removeEventListener(MouseEvent.MOUSE_DOWN, hideList);
			_list.view.visible = false;
		}
		
		/////////////////////////////////////
		//evt handler
		/////////////////////////////////////
		
		private function onValueButtonClick(e:MouseEvent):void
		{
			if (_list.view.visible == true)
			{
				_list.view.visible = false;
				return;
			}
			
			_list.view.visible = true;
			view.stage.addEventListener(MouseEvent.MOUSE_DOWN,hideList);
		}
		
		private function hideList(e:MouseEvent):void 
		{
			if (view.hitTestPoint(e.stageX, e.stageY)) return;
			
			view.stage.removeEventListener(MouseEvent.MOUSE_DOWN, hideList);
			_list.view.visible = false;
		}
		
		
		/////////////////////////////////////
		//getters/setters
		/////////////////////////////////////
		
		/** 当前选中数据 **/
		public function get selectDataItem():Object { return _list.selectedDataItem;}		
		
		/** 当前选中索引 **/
		public function get selectIndex():int { return _list.selectedIndex}
		
		/** 源数据 **/
		public function get array():Array { return _list.array;}
		public function set array(value:Array):void 
		{
			_list.array=value;
		}
		
		/** 顶部值显示按钮数据注入规则 **/
		public function get valueButtonInjectMethod():Function { return valueButton.injectMethod; }
		public function set valueButtonInjectMethod(value:Function):void
		{ 
			valueButton.injectMethod=value; 
		}
		
		/** 列表控件 **/
		public function get list():List { return _list; } 
		
	}

}